!function ($) {
    $.aToast = function () { }
    $.aAlert = function () { }
    $.aConfirm = function () { }

    // Toast
    $.aToast.prototype.show = function (options) {
        if (this.instance) {
            this.hide();
        }
        $.aToast.instance = this;
        let text = options.text || '正在加载';
        let duration = options.duration;
        this.showFn = options.onShow;
        this.hideFn = options.onHide;
        let loading = options.loading;

        let toast = document.createElement('div');
        let innerHTMLArr = ['<div class="toast_mask"><div class="component_toast">'];
        if (loading) {
            innerHTMLArr.push('<div><img src="" ' +
                'width="26%" /></div>');
        }
        innerHTMLArr.push('<div style="font-size: 16px;margin:10px 0;">' + text + '</div></div></div>');
        toast.innerHTML = innerHTMLArr.join('');
        document.body.appendChild(toast);

        // 开启事件
        this.showFn && this.showFn();
        // 如果有全局事件，则执行
        this.onShow && this.onShow();

        this.instance = toast;

        if (duration) {
            setTimeout(function () {
                $.aToast.instance.hide();
            }, duration);
        }
    }
    $.aToast.prototype.hide = function () {
        let instance = this.instance;
        if (instance && instance.parentNode) {
            instance.parentNode.removeChild(instance);
        }

        // 关闭事件
        this.hideFn && this.hideFn();
        // 如果有全局事件，则执行
        this.onHide && this.onHide();
    };
    // Alert
    $.aAlert.prototype.show = function (options) {
        if (this.instance) {
            this.hide();
        }
        $.aAlert.instance = this;
        let title = options.title || '';
        let content = options.content || '';
        let btnText = options.btnText || '确定';
        this.hideFn = options.onHide;
        this.showFn = options.onShow;

        let alert = document.createElement('div');
        alert.innerHTML = ['<div class="component_mask">',
            '<div class="component_dialog">',
            '<div class="component_dialog_hd"><strong class="component_dialog_title">' + title + '</strong></div>',
            '<div class="component_dialog_bd">' + content + '</div>',
            '<div class="component_dialog_ft">',
            '<a class="component_btn component_btn_dialog primary" onclick="aAlert.btnClick()">' + btnText + '</a>',
            '</div></div></div>'].join('');

        document.body.appendChild(alert);

        // 开启事件
        this.showFn && this.showFn();
        // 如果有全局事件，则执行
        this.onShow && this.onShow();

        this.instance = alert;
    };
    $.aAlert.prototype.hide = function () {
        let instance = this.instance;
        if (instance && instance.parentNode) {
            instance.parentNode.removeChild(instance);
        }
        // 关闭事件
        this.hideFn && this.hideFn();
        // 如果有全局事件，则执行
        this.onHide && this.onHide();
    };
    $.aAlert.btnClick = function () {
        let instance = $.aAlert.instance;
        instance.hide();
    };
    // Confirm
    $.aConfirm.prototype.show = function (options) {
        if (this.instance) {
            this.hide();
        }
        let defaultBtns = [{ text: '取消', class: 'confirm' }, { text: '取消', class: 'cancel' }];
        $.aConfirm.instance = this;
        let title = options.title || '';
        let content = options.content || '';
        this.hideFn = options.onHide;
        this.showFn = options.onShow;
        this.close = true;

        // 如果配置项没有进行配置则使用默认的
        if (!!options.btns) {
            let tempBtns = options.btns.slice(0, 2);
            defaultBtns.forEach(function (v, i) {
                tempBtns[i] = tempBtns[i] || v;
                for (let j in v) {
                    tempBtns[i][j] = tempBtns[i][j] || v[j];
                }
            });
            this.btns = tempBtns.concat(options.btns.slice(2));
        } else {
            this.btns = defaultBtns;
        }

        let confirm = document.createElement('div');
        let btnHtml = '';
        this.btns.forEach(function (v, i) {
            btnHtml += '<a class="component_btn ' + (v.class || '') + '" style="cursor: pointer;" onclick="aConfirm.btnClick(' + i + ', event)">' + (v.text || '确定') + '</a>'
        });
        confirm.innerHTML = ['<div class="component_mask">',
            '<div class="component_dialog" id="ConfirmClass_ComfirmArea">',
            '<div class="component_dialog_hd"><strong class="component_dialog_title">' + title + '</strong></div>',
            '<div class="component_dialog_bd">' + content + '</div>',
            '<div class="component_dialog_ft">',
            btnHtml,
            '</div></div></div>'].join('');

        document.body.appendChild(confirm);

        // 开启事件
        this.showFn && this.showFn();
        // 如果有全局事件，则执行
        this.onShow && this.onShow();

        this.instance = confirm;
    };
    $.aConfirm.prototype.hide = function () {
        let instance = this.instance;
        if (instance && instance.parentNode) {
            instance.parentNode.removeChild(instance);
        }

        // 关闭事件
        this.hideFn && this.hideFn();
        // 如果有全局事件，则执行
        this.onHide && this.onHide();
    };
    $.aConfirm.btnClick = function (index) {
        let instance = $.aConfirm.instance;
        if (typeof instance.btns[index].callback === 'function') {
            instance.btns[index].callback(instance);
        }
        if (instance.close) {
            instance.hide();
        } else {
            instance.close = true;
        }
    };
}(window)